package com.yanqu.road.dao.impl.activity.feast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.FeastEquipmentData;
import com.yanqu.road.entity.activity.feast.data.FeastPveUserProperty;
import com.yanqu.road.entity.activity.feast.data.FeastUserData;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FeastUserDataDaoImpl extends TempDataObjectDao<FeastUserData> {

    public static String getFeastUserDataSql = "select * from `t_u_activity_feast_data` where `activityId`=? and `userId`=?";

    public FeastUserData getFeastUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getFeastUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parseData(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateFeastUserDataSql = "update `t_u_activity_feast_data` set property=?, tripodLv=?,tripodExp=?," +
            "tripodEquipment=?, powerVal=?,maxPowerVal=?,angerVal=?,practiceVal=?," +
            "practiceSpeed=?, breakCount=?,energy=?,lastBreakFailTime=?,lastEnergyRecTime=?,lastMeditateTime=?," +
            "stateId=?,equipmentMap=?,sectId=?,mountId=?,imageId=?,energyUseCount=?,totalUseEnergy=?,luckPoint=?,unreceivedLuckPoint=?,pvpEndRound=?,items=?,drawTimes=?,tempReward=? where activityId=? and userId=?" ;
    @Override
    protected boolean updateData(FeastUserData feastUserData) {
        feastUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTripodLv()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTripodExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getTripodEquipment())));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getPowerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getMaxPowerVal()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getAngerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getPracticeVal()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getPracticeSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getBreakCount()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastBreakFailTime()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastMeditateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getStateId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getEquipmentMap())));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getSectId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getMountId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getImageId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getEnergyUseCount()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getUnreceivedLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getPvpEndRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(feastUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(feastUserData.getTempReward())));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getUserId()));
        if (getDb().executeNoneQuery(updateFeastUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertFeastUserDataSql = "insert into `t_u_activity_feast_data`(activityId,userId,tripodLv,tripodExp,tripodEquipment,powerVal,maxPowerVal,angerVal,practiceVal,practiceSpeed,breakCount," +
            "energy,lastBreakFailTime,lastEnergyRecTime,lastMeditateTime,property,stateId,equipmentMap,sectId,mountId,imageId,energyUseCount,totalUseEnergy,luckPoint,unreceivedLuckPoint,pvpEndRound,items,drawTimes,tempReward" +
            ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(FeastUserData feastUserData) {
        feastUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTripodLv()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTripodExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getTripodEquipment())));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getPowerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getMaxPowerVal()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getAngerVal()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getPracticeVal()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getPracticeSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getBreakCount()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastBreakFailTime()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getLastMeditateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getStateId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(feastUserData.getEquipmentMap())));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getSectId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getMountId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getImageId()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getEnergyUseCount()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getUnreceivedLuckPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, feastUserData.getPvpEndRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(feastUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, feastUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(feastUserData.getTempReward())));
        if (getDb().executeNoneQuery(insertFeastUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected FeastUserData parseData(ResultSet rs) throws SQLException {
        FeastUserData feastUserData = new FeastUserData();
        feastUserData.setActivityId(rs.getInt("activityId"));
        feastUserData.setUserId(rs.getLong("userId"));
        feastUserData.setTripodLv(rs.getInt("tripodLv"));
        feastUserData.setTripodExp(rs.getInt("tripodExp"));
        feastUserData.setPowerVal(rs.getLong("powerVal"));
        feastUserData.setMaxPowerVal(rs.getLong("maxPowerVal"));
        feastUserData.setAngerVal(rs.getInt("angerVal"));
        feastUserData.setPracticeVal(rs.getLong("practiceVal"));
        feastUserData.setPracticeSpeed(rs.getInt("practiceSpeed"));
        feastUserData.setBreakCount(rs.getInt("breakCount"));
        feastUserData.setEnergy(rs.getInt("energy"));
        feastUserData.setStateId(rs.getInt("stateId"));
        feastUserData.setLastBreakFailTime(rs.getLong("lastBreakFailTime"));
        feastUserData.setLastEnergyRecTime(rs.getLong("lastEnergyRecTime"));
        feastUserData.setLastMeditateTime(rs.getLong("lastMeditateTime"));
        feastUserData.setSectId(rs.getInt("sectId"));
        feastUserData.setMountId(rs.getInt("mountId"));
        feastUserData.setImageId(rs.getInt("imageId"));
        feastUserData.setEnergyUseCount(rs.getInt("energyUseCount"));
        feastUserData.setTotalUseEnergy(rs.getInt("totalUseEnergy"));
        feastUserData.setLuckPoint(rs.getInt("luckPoint"));
        feastUserData.setUnreceivedLuckPoint(rs.getInt("unreceivedLuckPoint"));
        feastUserData.setPvpEndRound(rs.getInt("pvpEndRound"));
        String value = rs.getString("equipmentMap");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, FeastEquipmentData> tempMap = JSON.parseObject(value, new TypeReference<ConcurrentHashMap<Integer, FeastEquipmentData>>(){});
            feastUserData.setEquipmentMap(tempMap);
        }
        feastUserData.setLastMeditateTime(rs.getLong("lastMeditateTime"));
        feastUserData.setItems(PropertyHelper.parseStringToProperty(rs.getString("items")));
        feastUserData.setTripodEquipment(JSON.parseObject(rs.getString("tripodEquipment"), new TypeReference<FeastEquipmentData>() {}));
        feastUserData.setProperty(JSON.parseObject(rs.getString("property"), new TypeReference<FeastPveUserProperty>() {
        }));
        feastUserData.setDrawTimes(rs.getLong("drawTimes"));
        String tempReward = rs.getString("tempReward");
        if(!StringUtils.isNullOrEmpty(tempReward)) {
            feastUserData.setTempReward(PropertyHelper.parseStringToProperty(tempReward));
        }
        return feastUserData;
    }
}
